<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" >
				<el-form-item>
					<el-input v-model="keyword" placeholder="请输入操作人姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="keyQuery">查询</el-button>
				</el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
			</el-form>
		</el-col>
		<!--数据类型列表-->
		<el-table :data="systemDictionaryTypes" highlight-current-row v-loading="listLoading"
              :row-key="getRowKey" @row-dblclick="showDetail" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55" :reserve-selection="true">
			</el-table-column>
			<el-table-column type="index" label="序号" width="60">
			</el-table-column>
			<el-table-column prop="sn" label="数据字典英文" width="450"align="center">
			</el-table-column>
			<el-table-column prop="name" label="数据字典类型" width="450" align="center" :show-overflow-tooltip="true">
			</el-table-column>
			<el-table-column label="操作" width="150" align="center">
				<template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
          :page-size="pageSize"
          :current-page="currentPage"
          :total="totals"
          :page-sizes="[5, 8, 10]"
          style="float:right;">
			</el-pagination>
		</el-col>
    <!--添加修改对话框-->
    <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="数据字典英文" prop="sn">
          <el-input v-model="saveForm.sn" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="数据字典类型" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>

    <!--数据明细列表-->
    <el-dialog :title="title_t" :visible.sync="detailVisible_d" :close-on-click-modal="false">
      <!--工具条-->
      <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true" >
          <el-form-item>
            <el-input v-model="keyword_d" placeholder="请输入明细名称"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" v-on:click="keyQuery_d">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleAdd_d">新增</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-table :data="systemDictionaryDetails" highlight-current-row v-loading="listLoading_d"
                :row-key="getRowKey_d"  @selection-change="selsChange_d" style="width: 100%;">
        <el-table-column type="selection" width="55" :reserve-selection="true">
        </el-table-column>
        <el-table-column type="index" label="序号" width="60">
        </el-table-column>
        <el-table-column prop="name" label="数据字典明细名称" width="200"align="center">
        </el-table-column>
        <el-table-column label="操作" width="150" align="center">
          <template scope="scope">
            <el-button size="small" @click="handleEdit_d(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="handleDel_d(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--工具条-->
      <el-col :span="24" class="toolbar">
        <el-button type="danger" @click="batchRemove_d" :disabled="this.sels_d.length===0">批量删除</el-button>
        <el-pagination
            layout="total, sizes, prev, pager, next, jumper"
            @current-change="handleCurrentChange_d"
            @size-change="handleSizeChange_d"
            :page-size="pageSize_d"
            :current-page="currentPage_d"
            :total="totals_d"
            :page-sizes="[5, 8, 10]"
            style="float:right;">
        </el-pagination>
      </el-col>
      <!--添加修改对话框-->
      <el-dialog :title="title_d" :visible.sync="saveFormVisible_d"
                 append-to-body :close-on-click-modal="false">
        <el-form :model="saveForm_d" label-width="80px" :rules="saveFormRules_d" ref="saveForm_d">
          <el-form-item label="数据字典明细名称" prop="name">
            <el-input v-model="saveForm_d.name" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="数据字典明细id">
            <el-input v-text="typesId" auto-complete="off" v-></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="saveFormVisible_d = false">取消</el-button>
          <el-button type="primary" @click.native="saveSubmit_d" :loading="saveLoading_d">提交</el-button>
        </div>
      </el-dialog>
    </el-dialog>


	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getsystemDictionaryTypeListPage, removesystemDictionaryType, batchRemovesystemDictionaryType, editsystemDictionaryType, addsystemDictionaryType } from '../../api/api';

	export default {
    watch: {
      saveFormVisible(val) { // 当弹窗打开时
        // 重置表单，清空校验信息
        this.$nextTick(() => {
          //  延时到下一帧才开始，也就是页面刚刚渲染结束！！
          this.$refs.saveForm.clearValidate()
        })
      }
    },
    data() {
      return {
        // 类型分页相关数据
        currentPage: 1,
        pageSize: 5,
        totals: 0,
        systemDictionaryTypes: [], // 数据字典类型数据
        listLoading: false,
        title_t: "明细列表",
        // 明细相关数据
        detailVisible_d: false,
        currentPage_d: 1,
        pageSize_d: 5,
        totals_d: 0,
        typesId: null,
        listLoading_d: false,
        systemDictionaryDetails: [],// 数据字典明细数据
        keyword_d: '',
        title_d: '',
        saveForm_d: {
          id: null,
          name: '',
          typesId: null,
        },
        saveFormVisible_d: false,//界面是否显示
        saveLoading_d: false, // 加载框
        saveFormRules_d: { // 表单项校验

        },
        // 高级查询
        keyword: '',
        // 所有管理员
        admins: '',
        // 分页批量选中
        getRowKey(row){
          return row.id;
        },
        getRowKey_d(row){
          return row.id;
        },
        // 批量删除
        sels: [],//列表选中列
        sels_d: [],
        //类型对话框数据
        title: '',
        saveForm: {
          id: null,
          sn: '',
          name: '',
        },
        //明细对话框数据
        saveFormVisible: false,//新增界面是否显示
        saveLoading: false, // 加载框
        saveFormRules: { // 表单项校验
          sn: [
            {required: true, message: '请输入数据字典英文', trigger: 'blur'}
          ],
          name: [
            {required: true, message: '请输入数据字典类型', trigger: 'blur'}
          ],
        },
      }
    },
    methods: {
      // 获取数据字典明细
      showDetail(row){
        this.typesId = row.id;
        this.detailVisible_d = true;
        this.getsystemDictionaryDetail();
      },
      // 明细分页查询
      getsystemDictionaryDetail(){
        let param = {
          typesId: this.typesId,
          pageSize_d: this.pageSize_d,
          currentPage_d: this.currentPage_d,
          keyword_d: this.keyword_d
        };
        this.listLoading_d = true;
        this.$http.post("/systemDictionaryDetail", param).then(res => {
          this.systemDictionaryDetails = res.data.data
          this.totals_d = res.data.totals
          this.listLoading_d = false;
        })
      },
      //分页查询
      getsystemDictionaryTypes() {
        let param = {
          pageSize: this.pageSize,
          currentPage: this.currentPage,
          keyword: this.keyword
        };
        this.listLoading = true;
        this.$http.post("/systemDictionaryType", param).then(res => {
          this.systemDictionaryTypes = res.data.data
          this.totals = res.data.totals
          this.listLoading = false;
        })
      },
      // 高级查询
      keyQuery() {
        this.currentPage = 1
        this.getsystemDictionaryTypes();
      },
      //选择第几页时触发
      handleCurrentChange(currentPage) {
        this.currentPage = currentPage;
        this.getsystemDictionaryTypes();
      },
      //选择每页显示记录条数
      handleSizeChange(pageSize) {
        this.pageSize = pageSize;
        this.getsystemDictionaryTypes();
      },

      // 高级查询
      keyQuery_d() {
        this.currentPage_d = 1
        this.showDetail()
      },
      //选择第几页时触发
      handleCurrentChange_d(currentPage_d) {
        this.currentPage_d = currentPage_d;
        this.getsystemDictionaryDetail()
      },
      //选择每页显示记录条数
      handleSizeChange_d(pageSize_d) {
        this.pageSize_d = pageSize_d;
        this.showDetail()
      },
      //删除
      handleDel: function (index, row) {
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          this.$http.delete("/systemDictionaryType/" + row.id).then(res => {
            if (res.data.success) {
              let pageTotal = Math.ceil(this.totals / this.pageSize)
              if (this.currentPage != 1 && this.currentPage == pageTotal && (this.totals - 1) % this.pageSize == 0) {
                this.currentPage = this.currentPage - 1
              }
              this.getsystemDictionaryTypes();
            } else {
              this.$message.error("系统繁忙，请稍后重试！【500】")
            }
          }).catch(res => {
            this.$message.error("系统繁忙，请稍后重试！【400,404,405】")
          })
          this.listLoading = false;
        }).catch(() => { // 取消

        });
      },
      //删除
      handleDel_d: function (index, row) {
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading_d = true;
          this.$http.delete("/systemDictionaryDetail/" + row.id).then(res => {
            if (res.data.success) {
              let pageTotal = Math.ceil(this.totals_d / this.pageSize_d)
              if (this.currentPage_d != 1 && this.currentPage_d == pageTotal && (this.totals_d - 1) % this.pageSize_d == 0) {
                this.currentPage_d = this.currentPage_d - 1
              }
              this.getsystemDictionaryDetail()
            } else {
              this.$message.error("系统繁忙，请稍后重试！【500】")
            }
          }).catch(res => {
            this.$message.error("系统繁忙，请稍后重试！【400,404,405】")
          })
          this.listLoading_d = false;
        }).catch(() => { // 取消

        });
      },
      // 选中改变时
      selsChange: function (sels) {
        this.sels = sels.map(obj => obj.id);
      },
      selsChange_d: function (sels_d) {
        this.sels_d = sels_d.map(obj => obj.id);
      },
      //批量删除
      batchRemove: function () {
        var ids = this.sels
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          this.$http.patch("/systemDictionaryType", ids).then(res => {
            this.listLoading = false;
            if (res.data.success) {
              //计算总页数
              let totalPage = Math.ceil(this.totals / this.pageSize)
              if (this.currentPage > 1 && this.currentPage == totalPage && (this.totals - this.sels.length) % this.pageSize == 0) {
                this.currentPage = this.currentPage - 1;
              }
              this.getsystemDictionaryTypes();
            } else {
              this.$message.error("系统繁忙，请稍后重试![500]");
            }
          }).catch(res => {
            this.$message.error("系统繁忙，请稍后重试![400]");
          })
        }).catch(() => {
        });
      },
      //显示编辑界面
      handleEdit: function (index, row) {
        this.title = '编辑'
        this.saveForm = Object.assign({}, row);
        this.saveFormVisible = true;
        this.fileList = [];
      },
      //显示新增界面
      handleAdd: function () {
        this.title = '添加'
        this.saveForm = { // 重置数据
          id: null,
          sn: '',
          name: '',
        };
        this.saveFormVisible = true;
      },
      //提交按钮
      saveSubmit: function () {
        this.$refs.saveForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.saveLoading = true;
              this.$http.put("/systemDictionaryType", this.saveForm).then(res => {
                this.saveLoading = false;
                this.saveFormVisible = false;
                if (res.data.success) {
                  this.getsystemDictionaryTypes()
                  this.$message.success("操作成功！")
                } else {
                  this.$message.error("系统繁忙，请稍后重试![500]");
                }
              }).catch(res => {
                this.$message.error("系统繁忙，请稍后重试![400]");
              })
            });
          }
        });
      },

      //批量删除
      batchRemove_d: function () {
        var ids = this.sels_d
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading_d = true;
          this.$http.patch("/systemDictionaryDetail", ids).then(res => {
            this.listLoading_d = false;
            if (res.data.success) {
              //计算总页数
              let totalPage = Math.ceil(this.totals_d / this.pageSize_d)
              if (this.currentPage_d > 1 && this.currentPage_d == totalPage && (this.totals_d - this.sels.length) % this.pageSize_d == 0) {
                this.currentPage_d = this.currentPage_d - 1;
              }
              this.getsystemDictionaryDetail()
            } else {
              this.$message.error("系统繁忙，请稍后重试![500]");
            }
          }).catch(res => {
            this.$message.error("系统繁忙，请稍后重试![400]");
          })
        }).catch(() => {
        });
      },
      //显示编辑界面
      handleEdit_d: function (index, row) {
        this.title_d = '编辑明细'
        this.saveForm_d = Object.assign({}, row);
        this.saveForm_d = { // 重置数据
          id: row.id,
          name: row.name,
          typesId: this.typesId
        };
        this.saveFormVisible_d = true;
      },
      //显示新增界面
      handleAdd_d: function () {
        this.title_d = '添加明细'
        this.saveForm_d = { // 重置数据
          id: null,
          name: '',
          typesId: this.typesId
        };
        this.saveFormVisible_d = true;
      },
      //提交按钮
      saveSubmit_d: function () {
        this.$refs.saveForm_d.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.saveLoading_d = true;
              this.$http.put("/systemDictionaryDetail", this.saveForm_d).then(res => {
                this.saveLoading_d = false;
                this.saveFormVisible_d = false;
                if (res.data.success) {
                  this.getsystemDictionaryDetail()
                  this.$message.success("操作成功！")
                } else {
                  this.$message.error("系统繁忙，请稍后重试![500]");
                }
              }).catch(res => {
                this.$message.error("系统繁忙，请稍后重试![400]");
              })
            });
          }
        });
      },
    },
		mounted() {
			this.getsystemDictionaryTypes();
		}
	}

</script>

<style scoped>

</style>